import 'dart:developer';

import 'package:base/base_lib.dart';
import 'package:favorites/res/lang.dart';
import 'package:favorites/router.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:inbox/res/lang.dart';
import 'package:inbox/router.dart';
import 'package:login/res/lang.dart';
import 'package:login/router.dart';
import 'package:more/res/lang.dart';
import 'package:more/router.dart';
import 'package:search/res/lang.dart';
import 'package:search/router.dart';
import 'package:trips/res/lang.dart';

import '/res/lang.dart';
import '/routers.dart';
import 'app_view.dart';
import 'permission_controller.dart';

class Application extends StatelessWidget {
  Application({Key? key}) : super(key: key);

  print(String text, {bool isError = false}) {
    log('** $text, isError [$isError]');
  }

  final lightTheme = ThemeData(
      primaryColor: KColors.primaryColor,
      appBarTheme: const AppBarTheme(
          backgroundColor: KColors.c_FFFFFF,
          toolbarTextStyle: TextStyle(color: KColors.c_333333)));
  final darkTheme = ThemeData(
      primaryColor: KColors.primaryColor,
      appBarTheme: const AppBarTheme(
          backgroundColor: KColors.c_333333,
          toolbarTextStyle: TextStyle(color: KColors.c_FFFFFF)));

  @override
  Widget build(BuildContext context) {
    return AppView(
      builder: (locale, builder) => GetMaterialApp(
        debugShowCheckedModeBanner: false,
        enableLog: true,
        navigatorKey: navigatorKey,
        builder: builder,
        logWriterCallback: print,
        translations: initLang([
          AppLang(),
          LoginLang(),
          SearchLang(),
          FavoritesLang(),
          InBoxLang(),
          TripsLang(),
          MoreLang()
        ]),
        fallbackLocale: fallbackLocale,
        locale: locale,
        localeResolutionCallback: (locale, list) {
          Get.locale ??= locale;
        },
        theme: Get.isDarkMode ? darkTheme : lightTheme,
        localizationsDelegates: const [
          GlobalCupertinoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
        supportedLocales: const [
          Locale('zh', 'CN'),
          Locale('en', 'US'),
        ],
        getPages: initRoute([
          AppRouter(),
          LoginRouter(),
          MoreRouter(),
          FavoritesRouter(),
          InboxRouter(),
          SearchRouter(),
        ]),
        initialBinding: InitBinding(),
        initialRoute: AppRouter.SPLASH,
      ),
    );
  }
}

class InitBinding extends Bindings {
  @override
  void dependencies() {
    Get.put<PermissionController>(PermissionController());
  }
}
